$(function() {
	/**
	 *  获取购物车数据
	 */
   
	let cl = $(".sj");
    let  sum=0;  
	let t = cl.children().remove();

	$.get("gwcsel.do", function(data) {
		// item.foodCarName
		// item.foodCarNumber
		// item.foodCarPriceCount
		//item.foodCarId
		$.each(data, function(i, item) {
			let th = "<th scope=\"row\" class=\"name\">" + item.foodCarName + "</th>"
			let td = "<td class=\"danjia\">￥"+item.foodCarPrice+"</td>"
			let td1 = "<td><a class=\"btn btn-default jian\" href=\"javascript:void(0)\" role=\"button\">-</a><input type=\"text\" value=\""+item.foodCarNumber+"\" style=\"width: 30px;height: 30px; text-align: center;\" class=\"tex\"/><a class=\"btn btn-default jia\" href=\"javascript:void(0)\" role=\"button\">+</a></td>"
			let td2 = "<td class=\"count\">￥"+item.foodCarPriceCount+"</td>"
			let  td3="<td class=\"id\"><input type=\"hidden\" value=\""+item.foodCarId+"\" style=\"width: 30px;height: 30px; text-align: center;\" class=\"tex\"/></td>"
            let  tr="<tr>"+th+td+td1+td2+td3+"</tr>"
             sum=sum+Number(item.foodCarPriceCount);
			 cl.append(tr) 

		});
	     $(".money").text("应付金额￥"+sum)

	}, "json")
	
	 /**
	  *  计算加的钱
	  */
		$("body").on("click",".jia",function(){
			 
		let num=$(this).prev().attr("value");
		let num1=Number(num)
	    $(this).prev().attr("value",num1+1)
	   
	    
	     //加钱
	  let sum=    $(this).parent().prev().text().substring(1);
		 let sum1=Number(sum)
		 //计算总价
		/* let sum=   $(this).parent().next().text().substring(1) 
		 let sum1=Number(sum);*/
		 let  c=  $(this).prev().attr("value")
		 let c1=Number(c)
		 let zongjia=(sum1*c1).toFixed(2); 
		 //赋值给总价
		$(this).parent().next().text("￥"+zongjia)
		  //获取id
		 let id=Number($(this).parent().next().next().children().attr("value"))
		 
		 //
		 let s=0;
		   //更改购物车数据
		      $.get("pay.do", {count: c,pricesum:zongjia,id:id }, function(data) {
		    	 // gw();
		    	  //计算所有商品总价格
		    		$.each(data, function(i, item) {
		    		
		    			 s=s+Number(item.foodCarPriceCount)
		    		});
		    		   $(".money").text("应付金额￥"+s)
			},"json")
		})
		
		/**
		 *  计算减的钱
		 */
			$("body").on("click",".jian",function(){
			 
		  let num=$(this).next().attr("value");
		  let num1=Number(num)
		  if(num1<=1){
			  
			  return false;
		  }else{
			   $(this).next().attr("value",num1-1)
		  }
	   
	   
	    
	     //减钱
	     let sum= $(this).parent().next().text().substring(1);
		 let sum1=Number(sum)
		 //原价
		 let  yuanjia= $(this).parent().prev().text().substring(1);
		 let yuanjia1=Number(yuanjia) 
		 let zong=(sum1-yuanjia1).toFixed(2);

		 //赋值给总价
		$(this).parent().next().text("￥"+zong)
		
		
		//获取id
			 let id=Number($(this).parent().next().next().children().attr("value"))
			 
			  let s=0;
		 
		  let num2=$(this).next().attr("value");
		  let num3=Number(num2)
		   //更改购物车数据
		      $.get("pay.do", {count: num3,pricesum:zong,id:id }, function(data) {
		    	 // gw();
		    	  //计算所有商品总价格
		    		$.each(data, function(i, item) {
		    		
		    			 s=s+Number(item.foodCarPriceCount)
		    		});
		    		   $(".money").text("应付金额￥"+s)
			},"json")
		})

	  
	     /**
	      *  将购物车数据插入订单表并显示价格
	      */
		
		
		
	  
		
})
